package buildcode.language.es.command;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import buildcode.build.ICommandExecute;
import buildcode.config.EntityExportConfig;
import buildcode.database.DataBaseTable;
import buildcode.language.Type;
import buildcode.language.es.field.EsEnumField;
import buildcode.language.es.type.EsEnum;
import buildcode.language.es.type.EsEnumComposite;
import buildcode.util.CommonUtil;

public class EsEnumCommandExecute implements ICommandExecute {

    @Override
    public List<Type> execute(List<DataBaseTable> bases, EntityExportConfig config) {
        List<EsEnum> all = new ArrayList<EsEnum>();
        for (DataBaseTable base : bases) {
            List<EsEnum> esEnumList = base.getColumns().stream().filter(v -> v.getEnumColumnInfo() != null).map(v -> {
                EsEnum entity = new EsEnum(base.getCamelCaseName() + CommonUtil.firstUpper(v.getCamelCaseName()) + config.getSuffix(), config.getPackageName());
                entity.setRemark(base.getTableRemark() + " " + v.getColumnName() + " " + config.getSuffix());
                v.getEnumColumnInfo().getEnumValues().forEach((key, val) -> {
                    EsEnumField field = new EsEnumField(key, val, v);
                    entity.addField(field);
                });
                entity.setMetaData(base);
                return entity;
            }).collect(Collectors.toList());
            all.addAll(esEnumList);
        }
        if (all.isEmpty()) {
            return Collections.emptyList();
        }
        return Arrays.asList(new EsEnumComposite(config.getFileName(), config.getPackageName(), all));
    }

}
